import { event } from 'vue-gtag'
import { useDateFormat, useNow } from '@vueuse/core'
import router from '@/router'

export enum Status {
    unComplete = 0, // 未完成所有任务
    unReceive = 1, // 完成任务未领取圣杯
    isComplete = 2 // 完成任务且已领取圣杯
}

export const getAssetsFiles = (url: string) => {
    return new URL(`/src/assets/imgs/${url}`, import.meta.url).href
}

export const parseTime = () => {
    const formatted = useDateFormat(useNow(), 'YYYY-MM-DD')
    return formatted.value
}

export const uploadTaskEventToGA = (eventName: string): void => {
    event(eventName)
}

export const encipherEmail = (email: string) => {
    const end = email.indexOf('@')
    const start = end - 5
    const encipherStr = email.slice(start, end)
    const newEmail = email.replace(encipherStr, '✳✳✳✳✳')
    return newEmail
}

export const getCurrentClientTime = (): string => {
    const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')
    return formatted.value
}

export const handleToEggOrLottery = () => {
    // 跳转至砸蛋或抽奖页面, 优先跳转到砸蛋
    const toPageObj = [
        { name: 'news', path: '/news', probability: 0.35 },
        { name: 'games', path: '/gameCenter', probability: 0.25 },
        { name: 'egg', path: '/egg', probability: 0.20 },
        { name: 'turntable', path: '/turntable', probability: 0.20 },
    ]

    const random = Math.random()
    let cumulativeProbability  = 0
    let index = 0
    for (const i in toPageObj) {
        cumulativeProbability += toPageObj[i].probability
        if (random < cumulativeProbability) {
            index = Number.parseInt(i)
            break
        }
    }
    router.push(toPageObj[index].path)
}

export const isWhiteListUser = (device: string): boolean => {
    const whiteList = ['be659e7a738a4646', '82d6ba88f173faa1', 'open']
    return whiteList.includes(device)
}

export const isBlackListUser = (device: string): boolean => {
    const blackList = ['1c78052da1ad6390']
    return blackList.includes(device)
}

export const shuffleArray = (array: number[]) => {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1)); // 生成 0 到 i 之间的随机数
        [array[i], array[j]] = [array[j], array[i]]; // 交换 array[i] 和 array[j]
    }
    return array;
}